<?php
namespace Admin\Controller;
use Think\Controller;
class CodeController extends BaseController {
    public function index(){
    	$tb=$this->gettables();
    	$this->assign('tb',$tb);
        $this->display();
    }
    public function gettables(){
       $model=M();
       $tables=$model->query('show tables');
       $tb=$this->dearray('tables_in_'.C('DB_NAME'),$tables);
       $tb=empty($tb)?'':$tb;
       return $tb;
    }

    /**
     * *
     * @return [type] [获取列表]
     */
    public function getfields(){
       $tbname=I('tbname');
       $model=M();
       $field=$model->query('SHOW FULL COLUMNS FROM `'.$tbname.'`');
       $index="";
       foreach ($field as $value) {
       	   if($value['key']=='PRI') $index= $value['field'];
       }
       $p=explode('_',$index);
       $prefix=$p[0];
       //echo $prefix.'_';
       //$this->jsonReturn($field);
       $array=array(
            'tbName'=>I('tbname'),
            'prefix'=>$prefix.'_',
            'field'=>$field,
            'codefield'=>base64_encode(json_encode($field))
       	);
       echo json_encode($array);
    }
    /**
     * **获取代码或者生成的方法
     * @return [type] [description]
     */
    public function getCode(){
         $model=D('Node');
         $model->makefile();
    }
    
    /**
     * *将数组降级
     * @param  [type] $id    [键值]
     * @param  [type] $array [数组]
     * @return [type]        [description]
     */
    public function dearray($id,$array){
       $return=array();
       foreach($array as $value){
          $return[]=$value[$id];  
       }
       return $return;
    }
    public function jsonReturn($data){
       if(empty($data)){
           $return=array(
                'status'=>'error',
                'msg'=>'数据获取失败',
                'data'=>NULL,
           	);
       }else{
       	    $return=array(
                 'status'=>'success',
                 'msg'=>'数据获取成功',
                 'data'=>$data
       	    );
       }
       echo json_encode($return); exit();
    }
   

    
}